package com.googlecode.connectlet.crypto.stream;

import java.io.OutputStream;

import com.googlecode.connectlet.crypto.cipher.Encipheror;

public class CfbDecipheror extends FeedbackCipher {
	private static final long serialVersionUID = 1L;

	private byte[] chain;

	public CfbDecipheror(Encipheror cipher, byte[] iv) {
		super(cipher, iv);
		chain = new byte[blockSize];
	}

	@Deprecated
	@Override
	public int output() {
		throw new UnsupportedOperationException();
	}

	@Deprecated
	@Override
	public void output(byte[] b, int off, int len) {
		throw new UnsupportedOperationException();
	}

	@Override
	public int update(int b) {
		chain[blockOffset] = (byte) b;
		int result = block[blockOffset] ^ b;
		blockOffset ++;
		if (blockOffset == blockSize) {
			blockOffset = 0;
			cipher.encrypt(chain, 0, block, 0);
		}
		return result;
	}

	@Deprecated
	@Override
	public OutputStream encrypt(OutputStream out) {
		throw new UnsupportedOperationException();
	}
}